const controllers = require('../../controllers/controllerIndex.js').controller;
Component({
  properties: {
    isshow:{
      type:Boolean,
      default:false
    },
    modeltype:{
      type:Number,
      default:1,
      observer(newval,oldval){
          this.setData({
            shopTypeListid:'',
            shopTypeListindex:0,
            selectByTypeIddata:[],
            selectByTypeIdindex:0,
            selectGoodsTempdata:[],
            selectGoodsTempindex:-1,
            goodsTempDetaildata:'',
            selectGoodsTempid:'',
            selectGoodsTempitem:'',
          })
        // if(newval!=oldval){
          this.shopTypeList()
        // }
      }
    },
  },
  data: {
    icon1:"https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/image/goodsManagement/select_94.png",
    icon2:"https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/image/goodsManagement/unchecked_94.png",
    shopTypeListindex:0,
    shopTypeList:[],
    shopTypeListid:'',
    selectByTypeIddata:[],
    selectByTypeIdindex:0,
    selectByTypeIdid:'',
    selectGoodsTempdata:[],
    selectGoodsTempindex:-1,
    selectGoodsTempid:'',
    selectGoodsTempitem:'',
    isloading:true,
    goodsTempDetaildata:'',
    selectGoodsList:[],
    templateIndex:0,
    type:"",
    name:"",
    selectAll:false,
    typeIndex:0
  },
  ready() {
      // this.shopTypeList()
  },
  methods: {
    //右侧内容
    shopTypeList(){
      var that =this;
      controllers.shopTypeList().then(res =>{
        let typeId=wx.getStorageSync('typeId')

        that.setData({
          shopTypeListdata: res.data,
          shopTypeListid:typeId,
          typeId:typeId
        })
        console.log(res.data)
        for(let i=0;i<res.data.length;i++){
          if(res.data[i].id==typeId){
            this.setData({
              typeIndex:i
            })
          }
        }
        that.selectByTypeId()
      })
    },
    // shopInfo(){
    //   var that =this;
    //   controllers.shopInfo({shopId:wx.getStorageSync('shopId')}).then(res =>{
    //     that.setData({
    //       shopTypeListdata: [res.data]
    //     })
    //     that.selectLabel()
    //   })
    // },
    selectByTypeId(){
      var that =this;
      controllers.selectByTypeId(that.data.shopTypeListid).then(res =>{
        if(res.data.length>0){
          that.setData({
            selectByTypeIddata: [{name:"全部",id:""}].concat(res.data),
            selectByTypeIdid:res.data[this.data.selectByTypeIdindex].id
          })
          console.log("二级tab")
          console.log(this.data.selectByTypeIddata)
          that.selectGoodsTemp()
        }
        
      })
    },
    // selectLabel(){
    //   controllers.selectLabel({
    //     type: 1,
    //     shopId:wx.getStorageSync('shopId'),
    //   }).then(res =>{
    //     this.setData({
    //       selectByTypeIddata:res.data,
    //       selectByTypeIdid:res.data[this.data.selectByTypeIdindex].id
    //     })
    //     this.selectGoodsTemp()
    //   })
    // },
    selectGoodsTemp(){
      var that =this;
      controllers.selectGoodsTemp({shopId:wx.getStorageSync('shopId'),labelId:that.data.selectByTypeIdid}).then(res =>{
        let lists=res.data;
        for(let i=0;i<lists.length;i++){
          lists[i].state=false
        }
        
        that.setData({
          selectGoodsTempdata:lists,
          isloading:false
        })
      }).catch(res=>{
        that.setData({
          isloading:false
        })
      })
    },
    bindTypeChange(e){
      let index=JSON.parse(e.detail.value)
      let id=this.data.shopTypeListdata[index].id
      this.setData({
        typeIndex:index,
        typeId:id
      })
      this.getshopTypeList(id,index)
    },
    getshopTypeList(id,index){
      // let id=e.currentTarget.dataset.id;
      // let index=e.currentTarget.dataset.index;
      this.setData({
        shopTypeListid:id,
        shopTypeListindex:index,
        selectByTypeIddata:[],
        selectByTypeIdindex:0,
        selectGoodsTempdata:[],
        selectGoodsTempindex:-1,
        goodsTempDetaildata:''
      })
      this.selectByTypeId()
    },
    getselectByTypeId(e){
      let id=e.currentTarget.dataset.id;
      let index=e.currentTarget.dataset.index;
      this.setData({
        selectByTypeIdid:id,
        selectByTypeIdindex:index,
        selectGoodsTempdata:[],
        selectGoodsTempindex:-1,
        goodsTempDetaildata:''
      })
      // this.selectGoodsTemp()
      this.searchSelectGoodsTemp()
    },
    getselectGoodsTemp(e){
      let item=e.currentTarget.dataset.item;
      let index=e.currentTarget.dataset.index;
      let list=this.data.selectGoodsTempdata
      if(list[index].state==false){
        list[index].state=true
      }else{
        list[index].state=false
      }
      this.setData({
        selectGoodsTempdata:list,
        selectGoodsTempitem:item,
        selectGoodsTempindex:index,
        goodsTempDetaildata:''
      })
      // this.goodsTempDetail()
    },

    //获取商品模板详情信息
    // goodsTempDetail(){
    //   controllers.goodsTempDetail({
    //     shopId:wx.getStorageSync('shopId'),
    //     goodsTempId:this.data.selectGoodsTempitem.id,
    //     goodsTemplateId:this.data.selectGoodsTempitem.id,
    //   }).then(res => {
    //     this.setData({
    //       goodsTempDetaildata:res.data
    //     })
    //     //上传模板
    //     this.uploadTemplate()
    //   })
    // },
    cancel(){
      this.setData({
        isshow:false
      })
    },
    //上传模板
    // uploadTemplate(){
    //   controllers.getSetByParentId({
    //     shopId:wx.getStorageSync('shopId'),
    //     labelId:goodsTempDetaildata.labelId,
    //     type:3,
    //     sessionId:wx.getStorageSync('token')

    //   }).then(res => {
    //       if(res.code==1){

    //         let data = {};

    //         data.shopId=wx.getStorageSync('shopId');
    //         data.labelId=res.data.id;
    //         data.state=goodsTempDetaildata.state;
    //         data.rewardState=goodsTempDetaildata.rewardState;
    //         data.integralState=goodsTempDetaildata.integralState;
    //         data.vip=goodsTempDetaildata.vip;
    //         data.type=goodsTempDetaildata.type;
    //         data.heat=goodsTempDetaildata.heat;
    //         data.oldPrice=goodsTempDetaildata.oldPrice;
    //         data.price=goodsTempDetaildata.price;
    //         data.name=goodsTempDetaildata.goodsName;
    //         data.content=goodsTempDetaildata.content || '';
    //         data.image=goodsTempDetaildata.image;
    //         data.integral=goodsTempDetaildata.integral || 0;
    //         data.rewardProportion=goodsTempDetaildata.rewardProportion || '';
    //         data.stock=goodsTempDetaildata.stock || '';
    //         data.warnCount=goodsTempDetaildata.warnCount || '';
    //         data.vipPrice=goodsTempDetaildata.vipPrice;
    //         data.rewardPrice=goodsTempDetaildata.rewardPrice || '';
    //         if(goodsTempDetaildata.detailsImage && goodsTempDetaildata.detailsImage.length>0){
    //           var detailsImage=[]
    //           for(let item of goodsTempDetaildata.detailsImage){
    //             detailsImage.push(item.image)
    //           }
    //           data.detailImgs=detailsImage.join(',') || '';
    //         }

    //         if(goodsTempDetaildata.rotationImage && goodsTempDetaildata.rotationImage.length>0){
    //           var rotationImage=[]
    //           for(let item of goodsTempDetaildata.rotationImage){
    //             rotationImage.push(item.image)
    //           }
    //           data.rotationImgs=rotationImage.join(',') || '';
    //         }


    //         controllers.saveGoods(data).then(res => {
    //           wx.showToast({
    //             title: '保存成功！',
    //             icon: 'success',
    //             duration: 2000
    //           })
    //           var that=this;
    //           setTimeout(res => {
    //             that.triggerEvent('loaddata');
    //             that.setData({
    //               isshow:false
    //             })
    //           })
    //         });

    //       }
    //   });
    // },



    //确认提交模板
    confirm(){
      let that=this
      let list=[]
      let value={}
      for(let i=0;i<this.data.selectGoodsTempdata.length;i++){
        console.log("外层执行")
        if(this.data.selectGoodsTempdata[i].state==true){
          console.log("执行")
          list.push(this.data.selectGoodsTempdata[i].id)
        }
      }
      console.log(list)
      console.log(this.data.selectGoodsTempdata)
      if(list.length<=0){
        wx.showToast({
          title: '请选择模板！',
          icon: 'none',
          duration: 2000
        })
        return
      }
      value.goodsTemplateIds=list.join(",");
      value.shopId=wx.getStorageSync('shopId')
      controllers.saveByTempIds(value).then(res=>{
        wx.showToast({
          icon:"none",
          title: '添加成功',
          duration:1500
        })
        setTimeout(function(){
          that.setData({
            isshow:false
          })
          wx.redirectTo({
            url: '/pages/goodsManagement/goodsManagementIndex/goodsManagementIndex',
          })
        },1500)
      })
    },


    //选择模板种类
    selectTemplateType(e){
      let index=e.currentTarget.dataset.index
      this.setData({
        templateIndex:index
      })
      if(index==0){
        this.setData({
          type:''
        })
      }else if(index==1){
        this.setData({
          type:1
        })
      }else if(index==2){
        this.setData({
          type:0
        })
      }
    },

    //查询获取新增商品模板
    searchSelectGoodsTemp(){
      let that=this
      let value={}
      value.shopId=wx.getStorageSync('shopId')
      value.labelId=this.data.selectByTypeIdid
      value.typeId=this.data.typeId
      value.type=this.data.type
      value.name=this.data.name
      controllers.selectGoodsTemp(value).then(res =>{
        let lists=res.data;
        for(let i=0;i<lists.length;i++){
          lists[i].state=false
        }
        
        that.setData({
          selectGoodsTempdata:lists,
          isloading:false
        })
      }).catch(res=>{
        that.setData({
          isloading:false
        })
      })
    },

    //获取商品名字
    getName(e){
      let name=e.detail.value;
      this.setData({
        name:name
      })
    },

    //选择所有模板商品
    selectAllGoods(){
      let list=this.data.selectGoodsTempdata
      if(this.data.selectAll==true){
        this.setData({
          selectAll:false
        })
        for(let i=0;i<list.length;i++){
          list[i].state=false
        }
      }else{
        this.setData({
          selectAll:true
        })
        for(let i=0;i<list.length;i++){
          list[i].state=true
        }
      }
      this.setData({
        selectGoodsTempdata:list
      })
    }
  }
})